var baseConfig = require("../../baseConfig")
var utils = require("../../utils/utils");
Page({
  data: {
    hiddenmodalput: true,
    hidden: true, //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
    is_hidden: 0,
    items: [{
        name: '银行卡',
        value: '1',
        checked: 'true'
      },
    ]
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    if (e.detail.value == '银行卡') {
      this.setData({
        payType: 2
      })
    } else {
      this.setData({
        payType: 1
      })
    }
  },
  //点击按钮弹窗指定的hiddenmodalput弹出框
  modalinput: function () {
    // this.setData({
    //   status: 1
    // })
    console.log('status', this.data.status);
    if (this.data.status == 1) {
      this.setData({
        hiddenmodalput: true,
        hidden: false
      })
    } else {
      this.setData({
        hiddenmodalput: false,
        // hidden: false
      })
    }
  },
  //取消按钮
  cancel: function (e) {
    this.setData({
      hiddenmodalput: true,
      hidden: false
    });
  },
  cancel1: function (e) {
    this.setData({
      hiddenmodalput: true,
      hidden: false
    });
    console.log(this.data);
  },
  close: function (e) {
    console.log('取消提现');
    this.setData({
      hidden: true,
      tixian: '',
    });
  },
  //确认
  confirm: function () {
    this.setData({
      hiddenmodalput: true,
      is_hidden: 0
    })
    wx.navigateTo({
      url: "/pages/toPayCard/toPayCard"
    })
  },
  confirm1: function () {
    let that = this
    this.setData({
      hiddenmodalput: false,
      hidden: true,
      is_hidden: 2
    })
    wx.request({
      url: baseConfig.baseUrl + 'tixianReq',
      method: 'POST',
      data: {
        payType: this.data.payType,
        amount: this.data.amount,
        accountType: this.data.accountType,
        payId: this.data.payId,
        aliPayAccount: this.data.aliPayAccount,
        bankCardNum: this.data.bankCardNum1,
        bankName: this.data.bankName,
        isCompany: this.data.isCompany,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'UID': this.data.uid
      },
      success: res => {
        console.log(res.data.data);
        if (res.data.data.error !== 0) {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                that.close()
              }
            }
          })
        } else {
          wx.showModal({
            title: '提现成功，预计十五个工作日内到账！',
            confirmText: "知道了",
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                //点击确定按钮
                console.log('点击确定按钮');
                that.close();
                wx.redirectTo({
                  url: '/pages/myWallet/myWallet',
                })
              } else if (res.cancel) {
                //点击取消按钮
              }
            }
          })
        }
      }
    })
  },
  confirmClick: function () {
    var reg = /^([0]|[1-9][0-9]*)$/g;
    if (this.data.active == 0) {
      this.setData({
        amount: 10
      })
    } else if (this.data.active == 1) {
      this.setData({
        amount: 20
      })
    } else if (this.data.active == 2) {
      this.setData({
        amount: 50
      })
    } else if (this.data.active == 3) {
      this.setData({
        amount: 100
      })
    }
    if (!(reg.test(this.data.amount))) {
      wx.showToast({
        title: '请输入整数金额，不含小数点',
        icon: 'none',
        duration: 2000
      })
    } else {
      this.setData({
        hiddenmodalput: false,
        hidden: false,
        is_hidden: 1
      })
      if (this.data.payType == undefined) {
        this.setData({
          payType: 1
        })
      }
    }

    console.log('选择提现类型payType：', this.data.payType,
      '用户IDuid：', this.data.uid,
      '是否为企业账户isCompany', this.data.isCompany,
      '提现金额amount：', this.data.amount,
      '账户类型accountType：', this.data.accountType,
      '支付id payId：', this.data.payId,
      '微信支付账号aliPayAccount：', this.data.aliPayAccount,
      '银行卡号bankCardNum：', this.data.bankCardNum1,
      '银行名称bankName：', this.data.bankName, );
  },
  bindfocus() { // 输入提现金额时取消上方选中
    // console.log('111');
    this.setData({
      active: 10000,
    })
  },
  tixianClick(e) {
    console.log(e.detail.value);
    this.setData({
      amount: e.detail.value
    })
  },
  toClick() {
    this.setData({
      active: 0
    })
  },
  toClick1() {
    this.setData({
      active: 1
    })
  },
  toClick2() {
    this.setData({
      active: 2
    })
  },
  toClick3() {
    this.setData({
      active: 3
    })
  },
  next() {
    console.log('下一步');
    this.setData({
      tixian: 1,
      // hiddenmodalput: false,
      // hidden: false,
      // is_hidden: 1
    })
  },
  toWithdraw: function (options) { // 跳转至提现页面
    console.log('跳转至提现页面', options);
  },
  toPayCard: function (e) { // 跳转至银行卡/支付宝
    wx.navigateTo({
      url: "/pages/toPayCard/toPayCard"
    })
  },
  toWithdrawalHistory: function (e) { // 跳转至提现记录
    wx.navigateTo({
      url: "/pages/toWithdrawalHistory/toWithdrawalHistory"
    })
  },
  toEarningsRecord: function (e) { // 跳转至收益记录
    wx.navigateTo({
      url: "/pages/toEarningsRecord/toEarningsRecord"
    })
  },
  onLoad: function (options) {
    let that = this
    var loginUserId = wx.getStorageSync("openid")
    var uid = wx.getStorageSync("userid")
    this.setData({
      uid
    })
    // console.log(loginUserId);
    wx.request({
      url: baseConfig.url + 'tuiwaApp/get_bank_alipay_list',
      method: 'POST',
      data: {
        loginUserId: loginUserId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        that.setData({
          bankName: res.data.data[0].bankName,
          bankCardNum: res.data.data[0].bankCardNum.replace(/\s/g, '').replace(/(\d{4})\d+(\d{4})$/, "**** **** **** $2"), // 将银行卡号前部分用 * 代替，仅显示后四位
          aliPayAccount: res.data.data[0].aliPayAccount,
          bankCardNum1: res.data.data[0].bankCardNum,
          trueName: res.data.data[0].trueName,
          status: res.data.data[0].status,
          accountType: res.data.data[0].accountType,
          payId: res.data.data[0].id,
          isCompany: res.data.data[0].isCompany
        })
      }
    })
    let integral = wx.getStorageSync('tuserInfo').integral;
    let totalIncomes = wx.getStorageSync('tuserInfo').totalIncomes;
    let integral1 = (integral).toFixed(2);
    let totalIncomes1 = (totalIncomes).toFixed(2);
    this.setData({
      integral: integral1,
      totalIncomes: totalIncomes1
    })
    this.getDateData()
  },
  onShow() {
    let that = this
    var loginUserId = wx.getStorageSync("openid")
    var uid = wx.getStorageSync("userid")
    this.setData({
      uid
    })
    // console.log(loginUserId);
    wx.request({
      url: baseConfig.url + 'tuiwaApp/get_bank_alipay_list',
      method: 'POST',
      data: {
        loginUserId: loginUserId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        that.setData({
          bankName: res.data.data[0].bankName,
          bankCardNum: res.data.data[0].bankCardNum.replace(/\s/g, '').replace(/(\d{4})\d+(\d{4})$/, "**** **** **** $2"), // 将银行卡号前部分用 * 代替，仅显示后四位
          aliPayAccount: res.data.data[0].aliPayAccount,
          bankCardNum1: res.data.data[0].bankCardNum,
          trueName: res.data.data[0].trueName,
          status: res.data.data[0].status,
          accountType: res.data.data[0].accountType,
          payId: res.data.data[0].id,
          isCompany: res.data.data[0].isCompany
        })
      }
    })
    let integral = wx.getStorageSync('tuserInfo').integral;
    let totalIncomes = wx.getStorageSync('tuserInfo').totalIncomes;
    let integral1 = (integral).toFixed(2);
    let totalIncomes1 = (totalIncomes).toFixed(2);
    this.setData({
      integral: integral1,
      totalIncomes: totalIncomes1
    })
    this.getDateData()
  },
  getData() {
    let that = this
    var openid = wx.getStorageSync('openid')
    console.log(openid, that.data.start_time, that.data.end_time, '11111111111');
    if (openid != undefined) {
      wx.request({
        url: baseConfig.url + 'tuiwaApp/get_earnings_record_data',
        method: 'POST',
        data: {
          loginUserId: openid,
          start_time: that.data.start_time,
          end_time: that.data.end_time,
          is_weixin: that.data.is_weixin,
          priceType: that.data.priceType,
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          let sumMonth = res.data.data.sumMonth
          let sumMonth1 = (sumMonth).toFixed(2);

          let sumLastMonth = res.data.data.sumLastMonth
          let sumLastMonth1 = (sumLastMonth).toFixed(2);

          let sumTiXian = res.data.data.sumTiXian
          let sumTiXian1 = (sumTiXian).toFixed(2);

          that.setData({
            sumEarnings: res.data.data.sumEarnings,
            yigeiEarnings: res.data.data.yigeiEarnings,
            earningsList: res.data.data.earningsList,
            sumLastMonth: sumLastMonth1,
            sumLastMonth1: sumLastMonth1,
            sumMonth: sumMonth1,
            sumMonth1: sumMonth1,
            sumTiXian: sumTiXian1,
            sumTiXian1: sumTiXian1,
          })
          let data = that.data.earningsList.map(r => r.day)
          console.log(data);
        }
      })
    }
  },
  getMonthFirstLastDay(year, month) {
    var firstDay = new Date(year, month - 1, 1); //这个月的第一天
    var currentMonth = firstDay.getMonth(); //取得月份数
    var lastDay = new Date(firstDay.getFullYear(), currentMonth + 1, 0); //是0而不是-1
    let start_time = utils.formatDate(firstDay, 'yyyy-mm-dd')
    let end_time = utils.formatDate(lastDay, 'yyyy-mm-dd')
    // console.log('first_last', start_time, end_time);
    this.setData({
      start_time: start_time,
      end_time: end_time,
    })
    // console.log('start_end', this.data.yearMonth, this.data.start_time, this.data.end_time)
    this.getData();
  },
  getDateData: function (e) { // 进入页面默认年月
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var Y = date.getFullYear(); // 获取年 
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); // 获取月
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); // 获取日
    const yearMonth = Y + '-' + M
    console.log(yearMonth);
    this.setData({
      yearMonth: yearMonth,
      date: yearMonth,
      year: Y,
      month: M,
      day: D,
    })
    this.getMonthFirstLastDay(Y, M)
  },
})